package cn.lihua.modules.framework.utils;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;

//https://www.cnblogs.com/christopherchan/p/11071098.html
public class JsonUtil {

    private final static Logger logger = LoggerFactory.getLogger(JsonUtil.class);

    //日期格式化
    private static final String STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss";

    private static ObjectMapper objectMapper;

    static{

        /**
         * ObjectobjectMapper是JSON操作的核心，Jackson的所有JSON操作都是在ObjectobjectMapper中实现。
         * ObjectobjectMapper有多个JSON序列化的方法，可以把JSON字符串保存File、OutputStream等不同的介质中。
         * writeValue(File arg0, Object arg1)把arg1转成json序列，并保存到arg0文件中。
         * writeValue(OutputStream arg0, Object arg1)把arg1转成json序列，并保存到arg0输出流中。
         * writeValueAsBytes(Object arg0)把arg0转成json序列，并把结果输出成字节数组。
         * writeValueAsString(Object arg0)把arg0转成json序列，并把结果输出成字符串。
         */
        objectMapper = new ObjectMapper();

        //对象的所有字段全部列入
        objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);

        //取消默认转换timestamps形式
        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false);
        
        //忽略空Bean转json的错误
        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS,false);

        //所有的日期格式都统一为以下的样式，即yyyy-MM-dd HH:mm:ss
        objectMapper.setDateFormat(new SimpleDateFormat(STANDARD_FORMAT));

        //忽略 在json字符串中存在，但是在java对象中不存在对应属性的情况。防止错误
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);

        objectMapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        objectMapper.setVisibility(PropertyAccessor.ALL, Visibility.ANY);
        
        //开启美化功能
        //objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
        
        //解决Java8 LocalDate,LocalDateTime等序列化问题
        JavaTimeModule module=new JavaTimeModule();
        module.addSerializer(LocalDateTime.class,new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        module.addDeserializer(LocalDateTime.class,new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        module.addSerializer(LocalDate.class,new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        module.addDeserializer(LocalDate.class,new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        objectMapper.registerModule(module);


    }

    /**
     * 对象转Json格式字符串
     * @param obj 对象
     * @return Json格式字符串
     */
    public static String toJSONString(Object o) {

        if (o == null) {
            return null;
        }

        if (o instanceof String)
            return (String) o;

        String jsonValue = null;
        try {
            jsonValue = objectMapper.writeValueAsString(o);
        } catch (JsonProcessingException e) {
            logger.error("Parse Object to String error",e);
        }

        return jsonValue;

    }

    @SuppressWarnings("unchecked")
    public static Map<String,Object> castToObject(String fromValue){
        if(fromValue == null || "".equals(fromValue) ){
            return null;
        }

        try {
            return objectMapper.readValue(fromValue, Map.class);
        } catch (Exception e) {
            logger.error("Parse String to Object error:", e);
            return null;
        }

    }

    /**
     * 字符串转换为自定义对象
     * @param str 要转换的字符串
     * @param clazz 自定义对象的class对象
     * @return 自定义对象
     */
    @SuppressWarnings("unchecked")
    public static <T> T castToObject(String fromValue, Class<T> clazz){
        if(fromValue == null || "".equals(fromValue) || clazz == null){
            return null;
        }

        try {
            return clazz.equals(String.class) ? (T) fromValue : objectMapper.readValue(fromValue, clazz);
        } catch (Exception e) {
            logger.error("Parse String to Object error:", e);
            return null;
        }

    }

    @SuppressWarnings("unchecked")
    public static <T> T castToObject(String fromValue, TypeReference<T> typeReference) {
        if (fromValue == null || "".equals(fromValue) || typeReference == null) {
            return null;
        }
        try {
            return (T) (typeReference.getType().equals(String.class) ? fromValue : objectMapper.readValue(fromValue, typeReference));
        } catch (IOException e) {
            logger.error("Parse String to Object error:", e);
            return null;
        }
    }
    
    public static <T> T castToObject(String fromValue, Class<?> collectionClazz, Class<?>... elementClazzes) {
        JavaType javaType = objectMapper.getTypeFactory().constructParametricType(collectionClazz, elementClazzes);
        try {
            return objectMapper.readValue(fromValue, javaType);
        } catch (IOException e) {
            logger.error("Parse String to Object error : ", e.getMessage());
            return null;
        }
    }
    
    public static <T> T getValue(String fromValue, Class<T> clazz){
    	return castToObject(fromValue,clazz);
    }
    
    public static <T> T getValue(String fromValue, TypeReference<T> toValueTypeRef){
    	return castToObject(fromValue, toValueTypeRef);
    }
    
    public static <T> T getValue(String fromValue, Class<?> collectionClazz, Class<?>... elementClazzes){
    	return castToObject(fromValue, collectionClazz, elementClazzes);
    }
    
    //可通过点语法获取数据，如getValue("data.list","xxxxxxxx",List.class);
    public static <T> T getValue(String key, String fromValue, Class<T> clazz){
    	Map<String,Object> infoMap = castToObject(fromValue);
    	if(infoMap == null) return null;
    	
    	return getValue(key, infoMap, clazz);
    }
    
    //可通过点语法获取数据，如getValue("data.list","xxxxxxxx",new TypeReference<List<User>>(){});
    public static <T> T getValue(String key, String fromValue, TypeReference<T> toValueTypeRef){
    	Map<String,Object> infoMap = castToObject(fromValue);
    	if(infoMap == null) return null;
    	
    	return getValue(key, infoMap, toValueTypeRef);
    }
    
    public static <T> T getValue(String key, String fromValue, Class<?> collectionClazz, Class<?>... elementClazzes){
    	Map<String,Object> infoMap = castToObject(fromValue);
    	if(infoMap == null) return null;
    	return getValue(key, infoMap, collectionClazz, elementClazzes);
    }
    
    //可通过点语法获取数据，如getValue("data.list",new TypeReference<List<User>>(){});
    @SuppressWarnings("rawtypes")
	public static <T> T getValue(String key, Map fromMap, Class<T> clazz){
    	
    	try {
    		
    		// 首先将key进行拆分
            String[] keys = key.split("[.]");
            
            for (int i = 0; i < keys.length; i++) {
            	
            	Object value = fromMap.get(keys[i]);
            	if(value == null) return null;
            	
            	if (i < keys.length - 1) {
            		fromMap = (Map) value;
                }else {
                    return objectMapper.convertValue(value, clazz);
                }
            }
            
        } catch (Exception e) {
            logger.error("getValue error : ", e.getMessage());
            return null;
        }
        
        return null;
        
    }
    
    @SuppressWarnings("rawtypes")
	public static <T> T getValue(String key, Map fromMap, Class<?> collectionClazz, Class<?>... elementClazzes){
    	try {
    		
        	// 首先将key进行拆分
            String[] keys = key.split("[.]");
            
            for (int i = 0; i < keys.length; i++) {
            	
            	Object value = fromMap.get(keys[i]);
            	if(value == null) return null;
            	
            	if (i < keys.length - 1) {
            		fromMap = (Map) value;
                }else {
                	JavaType javaType = objectMapper.getTypeFactory().constructParametricType(collectionClazz, elementClazzes);
                    return objectMapper.convertValue(value, javaType);
                }
            	
            }
            
        } catch (Exception e) {
            logger.error("getValue error : ", e.getMessage());
            return null;
        }
    	
        return null;
    }
    
    @SuppressWarnings("rawtypes")
	public static <T> T getValue(String key, Map fromMap, TypeReference<T> toValueTypeRef){
    	
    	try {
    		
        	// 首先将key进行拆分
            String[] keys = key.split("[.]");
            
            for (int i = 0; i < keys.length; i++) {
            	
            	Object value = fromMap.get(keys[i]);
            	if(value == null) return null;
            	
            	if (i < keys.length - 1) {
            		fromMap = (Map) value;
                }else {
                    return objectMapper.convertValue(value, toValueTypeRef);
                }
            	
            }
            
        } catch (Exception e) {
            logger.error("getValue error : ", e.getMessage());
            return null;
        }
    	
        return null;
        
    }
    
    /**
     * 将对像转换成具体的其他Bean对像
     * @param fromValue
     * @param toValueTypeRef
     * @return
     */
    public static <T> T convertValue(Object fromValue, TypeReference<T> toValueTypeRef){
    	
    	try {
    		return objectMapper.convertValue(fromValue, toValueTypeRef);
        } catch (Exception e) {
            logger.error("convertValue error : ", e.getMessage());
            return null;
        }
    	
    }
    
    public static <T> T convertValue(Object fromValue, Class<T> toValueType){
    	
    	try {
    		return objectMapper.convertValue(fromValue, toValueType);
        } catch (Exception e) {
            logger.error("convertValue error : ", e.getMessage());
            return null;
        }
    	
    }

    public static String getString(Map<String,Object> fromMap, String fieldName){
        return fromMap.get(fieldName)==null ? null : fromMap.get(fieldName).toString();
    }
    
    //根据filedName的key查找map为空时，使用对应的defaultValue默认值替换返回
    public static String getString(Map<String,Object> jsonObject, String fieldName,String defaultValue){
        return jsonObject.get(fieldName)==null ? defaultValue : jsonObject.get(fieldName).toString();
    }

    public static Integer getInteger(Map<String,Object> jsonObject, String fieldName){
        return jsonObject.get(fieldName)==null ? null : (Integer)jsonObject.get(fieldName);
    }
    
    public static Double getDouble(Map<String,Object> jsonObject, String fieldName){
        return jsonObject.get(fieldName)==null ? null : (Double)jsonObject.get(fieldName);
    }
    
    public static Boolean getBoolean(Map<String,Object> jsonObject, String fieldName){
        return jsonObject.get(fieldName)==null ? false : (Boolean)jsonObject.get(fieldName);
    }
    
    public static Long getLong(Map<String,Object> jsonObject, String fieldName){
        return jsonObject.get(fieldName)==null ? null : (Long)jsonObject.get(fieldName);
    }
    
    public static <T> List<T> getList(Map<String,Object> jsonObject, String fieldName,Class<T> clazz){
        return jsonObject.get(fieldName)==null ? null : JsonUtil.getValue(fieldName, jsonObject, List.class,clazz);
    }

}